// pages/sales/sales.js
const app = getApp()
const db = wx.cloud.database()
const _ = db.command

Page({

  /**
   * 页面的初始数据
   */
  data: {
    top_label: [
      {
        name: '添加',
        color: 'lightgreen'
      },
      {
        name: '删除',
        color: 'lightgreen'
      },
      {
        name: '修改',
        color: 'lightgreen'
      },
    ],
    stafflist: [],
    choicenumber: 0,
    choicevalue: 0,
    valuearray: [],
    p_choice: 0,
  },

  //三选一
  sanxuanyi(e) {
    //setData给动态下标数组的写法
    var index = "top_label[" + e.currentTarget.dataset.number + "].color"
    this.setData({
      'top_label[0].color': 'lightgreen',
      'top_label[1].color': 'lightgreen',
      'top_label[2].color': 'lightgreen',
      [index]: 'grey',
      p_choice: e.currentTarget.dataset.number
    })
    console.log(this.data.p_choice)
  },

  //勾选
  checkboxChange: function (e) {
    this.data.choicenumber = e.detail.value.length
    this.data.choicevalue = e.detail.value
    this.data.valuearray = e.detail.value
    console.log(e.detail.value,this.data.choicenumber)
  },

  //确认
  confirm() {
    var that = this
    // //确认操作者是否拥有管理员权限
    if (wx.getStorageSync('mobile') == '18601164781') {
      //修改
      if (that.data.p_choice == 2) {
        if (that.data.choicenumber == 1) {
          //转统计修改页面，
          wx.redirectTo({
            url: '/pages/salesxg/salesxg?name=' + that.data.choicevalue,
          })
        } else {
          wx.showModal({
            title: '提醒',
            content: '信息修改只能单选！',
            showCancel: false
          })
        }
      }
      //添加
      else if (that.data.p_choice == 0) {
        wx.redirectTo({
          url: '/pages/salesxg/salesxg',
        })
      }
      //删除
      else if (that.data.p_choice == 1) {
        db.collection('TeamLeader')
          .where({
            name: _.in(that.data.valuearray)
          })
          .remove({
            success(res) {
              console.log(res)
              wx.showModal({
                title: '提醒',
                content: '删除已完成！',
                showCancel: false
              })
              wx.redirectTo({
                url: '/pages/sales/sales',
              })
            }
          })
      }
    }else{
      wx.showModal({
        title: '提醒',
        content: '您没有相应权限！',
        showCancel: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this    
    //投放视频的总数量
    db.collection('TeamLeader')
      .get()
      .then(res=>{
        that.setData({
          stafflist: res.data
        })
        console.log(res)
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})